﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Willson.UCControl
{
    public class UCDataGridViewEnter : System.Windows.Forms.DataGridView 
    {
        private int rowIndex = 0;
        private int colIndex = 0;
        
        public UCDataGridViewEnter()
        {
            this.StandardTab = false;
        }

        protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
        {
            switch (keyData)
            {
                case System.Windows.Forms.Keys.Enter:
                    System.Windows.Forms.SendKeys.Send("{TAB}");
                    return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        protected override void OnCellEnter(DataGridViewCellEventArgs e)
        {
            if (this.Rows[e.RowIndex].Cells[e.ColumnIndex].ReadOnly || this.Rows[e.RowIndex].Cells[e.ColumnIndex].Visible == false)
            {
                if ( rowIndex != e.RowIndex || colIndex != e.ColumnIndex)
                    System.Windows.Forms.SendKeys.Send("{TAB}");
            }

            rowIndex = e.RowIndex;
            colIndex = e.ColumnIndex;
             
            base.OnCellEnter(e);
        }

    }
}
